package com.helps.controller;

import com.helps.db.Database;
import com.helps.util.RSAUtil;
import org.apache.commons.codec.cli.Digest;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.security.KeyPair;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping(value = "/test")
public class TestController {

    private static Logger logger = LogManager.getLogger(TestController.class);

    private Database database;

    private ModelAndView mv;

    public ModelAndView getMv() {
        return mv;
    }

    @Resource(name = "response")
    public void setMv(ModelAndView mv) {
        this.mv = mv;
    }

    public Database getDatabase() {
        return database;
    }

    @Resource(name = "database")
    public void setDatabase(Database database) {
        this.database = database;
    }

    @RequestMapping(value = "/info")
    @ResponseBody
    public ModelAndView info(HttpServletRequest request, HttpSession session) {

        session.setAttribute("username", "zhaoyan");

        System.out.println("This is test" + session.getId());

        /*
        List<Map<String, String>> data = getDatabase().fetchAll("show tables");
        System.out.println(data.get(0));
        for (Map<String, String> map : data) {
            System.out.println(map);
        }
        */

        Map<Integer, Object> params = new HashMap<Integer, Object>();
        params.put(1, 1);
        Map<String, String> account = getDatabase().fetchOne(
                "SELECT * FROM accounts where account_id = ?",
                params
        );

        System.out.println(request.getParameter("username"));

        mv.addObject("data", account);

        return mv;
    }

    @RequestMapping(value = "/user")
    @ResponseBody
    public ModelAndView user(HttpServletRequest request, HttpSession session) {

        String str = "sad,fasfasf,ah2kj3hr3r";
        System.out.println(str.substring(0, 3));
        str.length();
        String[] aa = str.split(",");
        System.out.println(aa[0]);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String strsss = simpleDateFormat.format(new Date());
        System.out.println(strsss);
        try {
            long time = simpleDateFormat.parse(strsss).getTime();
            System.out.println(time / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String md5str = DigestUtils.md5Hex("123123");
        String shastr = DigestUtils.sha1Hex("123123");
        //System.out.println(md5str);
        //System.out.println(shastr);

        KeyPair keyPair = null;
        try {
            keyPair = RSAUtil.getKeyPair();
            String publicKeyStr = RSAUtil.getPublicKey(keyPair);
            String privateKeyStr = RSAUtil.getPrivateKey(keyPair);
            //System.out.println("RSA公钥Base64编码:" + publicKeyStr);
            //System.out.println("RSA私钥Base64编码:" + privateKeyStr);

            Map<String, String> varevn = System.getenv();

            String sss = System.getProperty("os.version");
            System.out.println(sss);

            String filepath = request.getServletContext().getRealPath("/upload");
            File file = new File(filepath);
            if (!file.exists() && !file.isDirectory()) {
                file.mkdir();
            }
            System.out.println(filepath);
        } catch (Exception e) {
            e.printStackTrace();
        }

        logger.info("user controller");

        mv.addObject("data", "ssssss" + session.getAttribute("username"));

        return mv;
    }
}
